package com.directions.android;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.directions.android.navigation.EntryItem;
import com.directions.android.navigation.NavigationDrawerFragment;
import com.directions.android.navigation.NavigationDrawerFragment.NavigationDrawerCallbacks;
import com.directions.android.screens.MarkersFragment;
import com.directions.android.screens.InfoFragment;
import com.directions.android.screens.MapFragment;
import com.directions.android.screens.MenuFragment.OnShowMapListener;
import com.directions.android.screens.PreferenceFragment;

public class MainActivity extends ActionBarActivity implements
		NavigationDrawerCallbacks, OnShowMapListener {

	/**
	 * Fragment managing the behaviors, interactions and presentation of the
	 * navigation drawer.
	 */
	private NavigationDrawerFragment mNavigationDrawerFragment;

	/**
	 * Used to store the last screen title. For use in
	 * {@link #restoreActionBar()}.
	 */
	private CharSequence mTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		StrictMode.enableDefaults(); // Enable default violations logging.

		mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
				.findFragmentById(R.id.navigation_drawer);
		mTitle = getTitle();

		// Set up the drawer.
		mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
				(DrawerLayout) findViewById(R.id.drawer_layout));
	}

	// /////////////////////////
	// NAVIGATION FRAGMENTS
	// /////////////////////////

	@Override
	public void onNavigationDrawerItemSelected(EntryItem item, int position) {
		// update the main content by replacing fragments
		if (position < 6) {
			Fragment fragment = null;
			switch (position) {
			case 1:
				fragment = com.directions.android.screens.MenuFragment
						.getInstance();
				break;
			case 2:
				fragment = MapFragment.getInstance(null);
				break;
			case 3:
				fragment = MarkersFragment.getInstance();
				break;
			case 4:
				fragment = InfoFragment.getInstance();
				break;
			case 5:
				fragment = PreferenceFragment.getInstance();
				break;
			default:
				break;
			}
			beginTransition(fragment);
		} else {
			switch (position) {
			case 7:
			case 8:
			case 9:
				String res = null;

				switch (position) {
				case 7:
					res = getString(R.string.twitter);
					break;
				case 8:
					res = getString(R.string.facebook);
					break;
				case 9:
					res = getString(R.string.google_play);
					break;
				}

				Uri myUri = Uri.parse(res);
				Intent intent = new Intent(Intent.ACTION_VIEW, myUri);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
						| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
				startActivity(intent);
				break;
			case 10:
				Intent sharingIntent = new Intent(
						android.content.Intent.ACTION_SEND);
				sharingIntent.setType("text/plain");
				String shareBody = getString(R.string.share);
				sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
						shareBody);
				startActivity(Intent.createChooser(sharingIntent, "Share via"));
				break;
			}
		}
	}

	public void onSectionAttached(EntryItem entry, int number) {
		mTitle = entry == null ? getString(R.string.app_name) : entry
				.getTitle();
	}

	public void restoreActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(mTitle);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mNavigationDrawerFragment.isDrawerOpen()) {
			menu.clear();
			// Only show items in the action bar relevant to this screen
			// if the drawer is not showing. Otherwise, let the drawer
			// decide what to show in the action bar.
			getMenuInflater().inflate(R.menu.main, menu);
			restoreActionBar();
			return true;
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onShowMap(Bundle data) {
		// 2 is the map position
		mNavigationDrawerFragment.setmCurrentSelectedPosition(2);
		beginTransition(MapFragment.getInstance(data));
	}

	private void beginTransition(Fragment fragment) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		fragmentManager.beginTransaction().replace(R.id.container, fragment)
				.addToBackStack(null).commitAllowingStateLoss();

	}

}
